package com.ebt.m.policy.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.RecyclerFootManger;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.data.rxModel.api.PolicyAPI;
import com.ebt.m.data.rxModel.apibean.PolicyProposal;
import com.ebt.m.policy.view.PolicyProposalListView;
import com.ebt.m.proposal_v2.utils.Utils;
import com.ebt.m.utils.ConfigData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by andy on 17-5-31.
 */

public class PolicyProposalListPresenter extends BaseListPresenter<BaseListContract.IView> {
    private int currentPage = 1;
    private int pageSize = 10;
//    private int position;

    public PolicyProposalListPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        currentPage = 1;
        Map<String, String> params = null;
        if (args == null || args[0] == null) {
            params = new HashMap<>();
        } else {
            params = (Map<String, String>) args[0];
        }
        params.put("page", currentPage + "");
        params.put("pageSize", ConfigData.PAGE_COUNT_LIMIT + "");
        PolicyAPI opportunityApi = AppInitService.getPolicyApi();
        opportunityApi.getPolicyProposals(params)
                .filter(getFilterForFootviewStatus(true))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    Utils.print("计划书", ">>>客户计划书更新完成");
                    iView.updateData(createSectionItem(response.getData()));
//                    EventBus.getDefault().post(new DraftUpdateCompleteEvent()); // 通知草稿页面关闭
                }, throwable -> {
                    throwable.printStackTrace();
                    Utils.print("draft", ">>>草稿更新完成");
                    iView.errorData();
//                    EventBus.getDefault().post(new DraftUpdateCompleteEvent()); // 通知草稿页面关闭
                });
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
        Map<String, String> params = null;
        if (args == null || args[0] == null) {
            params = new HashMap<>();
        } else {
            params = (Map<String, String>) args[0];
        }
        params.put("page", currentPage + 1 + "");
        params.put("pageSize", ConfigData.PAGE_COUNT_LIMIT + "");
        PolicyAPI opportunityApi = AppInitService.getPolicyApi();
        opportunityApi.getPolicyProposals(params)
                .filter(getFilterForFootviewStatus(false))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    currentPage++;
                    iView.updateDataAdd(createSectionItem(response.getData()));
                }, throwable -> {
                    throwable.printStackTrace();
                    iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
                });
    }

    public List<SectionItem> createSectionItem(List list) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null || list.size() < 1) {
            return sections;
        }
        try {
            int s = 0;
            for (int i = 0; i < list.size(); i++) {
                PolicyProposal proposalItem = (PolicyProposal) list.get(i);
                s = proposalItem.proposalItems.size();
                for (int j = 0; j < 3 && j < s; j++) {
                    if (j == 0) {
                        SectionItem item = new SectionItem(PolicyProposalListView.VIEW_YTPE_TITLE, proposalItem);
                        sections.add(item);
                        item = new SectionItem(PolicyProposalListView.VIEW_TYPE_ITEM, proposalItem.proposalItems.get(0));
                        item.extras = proposalItem.name;
                        sections.add(item);
                    } else {
                        SectionItem item = new SectionItem(PolicyProposalListView.VIEW_TYPE_ITEM, proposalItem.proposalItems.get(j));
                        item.extras = proposalItem.name;
                        sections.add(item);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }
}
